package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ApprovalConfig;
import com.ruoyi.system.service.IApprovalConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 审批流配置Controller
 * 
 * @author ruoyi
 * @date 2025-10-21
 */
@RestController
@RequestMapping("/system/approvalConfig")
public class ApprovalConfigController extends BaseController
{
    @Autowired
    private IApprovalConfigService approvalConfigService;

    /**
     * 查询审批流配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:approvalConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(ApprovalConfig approvalConfig)
    {
        startPage();
        List<ApprovalConfig> list = approvalConfigService.selectApprovalConfigList(approvalConfig);
        return getDataTable(list);
    }

    /**
     * 导出审批流配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:approvalConfig:export')")
    @Log(title = "审批流配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ApprovalConfig approvalConfig)
    {
        List<ApprovalConfig> list = approvalConfigService.selectApprovalConfigList(approvalConfig);
        ExcelUtil<ApprovalConfig> util = new ExcelUtil<ApprovalConfig>(ApprovalConfig.class);
        util.exportExcel(response, list, "审批流配置数据");
    }

    /**
     * 获取审批流配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:approvalConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(approvalConfigService.selectApprovalConfigById(id));
    }

    /**
     * 根据模块代码获取审批流配置
     */
    @GetMapping(value = "/module/{moduleCode}")
    public AjaxResult getByModuleCode(@PathVariable("moduleCode") String moduleCode)
    {
        return success(approvalConfigService.selectByModuleCode(moduleCode));
    }

    /**
     * 检查模块是否启用审批
     */
    @GetMapping(value = "/enabled/{moduleCode}")
    public AjaxResult checkModuleEnabled(@PathVariable("moduleCode") String moduleCode)
    {
        boolean enabled = approvalConfigService.isModuleEnabled(moduleCode);
        return success(enabled);
    }

    /**
     * 新增审批流配置
     */
    @PreAuthorize("@ss.hasPermi('system:approvalConfig:add')")
    @Log(title = "审批流配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ApprovalConfig approvalConfig)
    {
        return toAjax(approvalConfigService.insertApprovalConfig(approvalConfig));
    }

    /**
     * 修改审批流配置
     */
    @PreAuthorize("@ss.hasPermi('system:approvalConfig:edit')")
    @Log(title = "审批流配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ApprovalConfig approvalConfig)
    {
        return toAjax(approvalConfigService.updateApprovalConfig(approvalConfig));
    }

    /**
     * 启用/停用审批流配置
     */
    @PreAuthorize("@ss.hasPermi('system:approvalConfig:edit')")
    @Log(title = "审批流配置", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody ApprovalConfig approvalConfig)
    {
        return toAjax(approvalConfigService.updateApprovalConfig(approvalConfig));
    }

    /**
     * 删除审批流配置
     */
    @PreAuthorize("@ss.hasPermi('system:approvalConfig:remove')")
    @Log(title = "审批流配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(approvalConfigService.deleteApprovalConfigByIds(ids));
    }
}
